<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<script type="text/javascript" src="${ctx }/datepicker/WdatePicker.js"></script>

<p>
    <span>筛选：</span>
    <span>
        开始日期：<input id="startDate" name="startDateStr" onfocus="WdatePicker({maxDate:$('#endDate').val()})" type="text" style="width: 80px" value="${startDateStr}"/>
        结束日期：<input id="endDate" name="endDateStr" onfocus="WdatePicker({minDate:$('#startDate').val()})" type="text" style="width: 80px" value="${endDateStr}"/>
    </span>

    <span id="org_sel"></span>
    <span>
        <select id="duty_sel" style="opacity: 0;">
            <option value="-1">所有职务</option>
        </select>
    </span>

    <input type="button" value="点击搜索" onclick="javascript:onSearchClick()">
</p>

<script type="text/javascript">
    $(function () {
        setTimeout('loadChildrenOrgsByTab(${organizationId})', 800);
        //loadChildrenOrgsByTab(${organizationId})
    })

    /**
     * Tab Change that load the child org
     * @param orgId
     */
    function loadChildrenOrgsByTab(orgId) {
        $("input[type='checkbox']").each(function () {
            $(this).attr("checked", false);
        });
        if ($("#all_select") != null) {
            $("#all_select").parent().attr("class", "");
        }
        //load the organization
        $.ajax({
            type:"get",
            url:"/ajax/organization-children.xhtml?parentId=" + orgId,
            //data: "name=John&location=Boston",
            dataType:"json",
            success:function (data) {
                var strHTML = "<div id=\"uniform-undefined-" + orgId + "\" class=\"selector\">";
                strHTML += "<span style=\"-moz-user-select: none;\">所有部门</span>";
                strHTML += "<select style='opacity: 0;' onchange='loadChildrenOrgs(this," + orgId + ")'>";
                strHTML += "<option value=\"-1\">所有部门</option>";
                if (data != null && data.length > 0) {
                    $.each(data, function (i, org) {
                        strHTML += "<option value=\"" + org.id + "\">" + org.fullName + "</option>";
                    });
                }
                strHTML += "</select></div>";
                $("#org_sel").html("");
                $("#org_sel").append(strHTML);
            }
        });
        // load the duty
        loadDutyByOrgId(orgId);
    }

    function loadChildrenOrgs(evtObj, parentId) {
        var evtObjId = evtObj.options[evtObj.selectedIndex].value;
        $(evtObj).prev().html($(evtObj).find("option:selected").text());
        $("#uniform-undefined-" + parentId).nextAll().remove();
        $.ajax({
            type:"get",
            url:"/ajax/organization-children.xhtml?parentId=" + evtObjId,
            //data: "name=John&location=Boston",
            dataType:"json",
            success:function (data) {
                if (data != null && data.length > 0) {
                    var strHTML = "<div id=\"uniform-undefined-" + evtObjId + "\" class=\"selector\">";
                    strHTML += "<span style=\"-moz-user-select: none;\">所有部门</span>";
                    strHTML += "<select style='opacity: 0;' onchange='loadChildrenOrgs(this," + evtObjId + ")'>";
                    strHTML += "<option value=\"-1\">所有部门</option>";
                    $.each(data, function (i, org) {
                        strHTML += "<option value=\"" + org.id + "\">" + org.fullName + "</option>";
                    });
                    strHTML += "</select></div>";
                    $("#uniform-undefined-" + parentId).after(strHTML);
                }
            }
        });
        //load duty list
        loadDutyByOrgId(evtObjId);
    }

    function loadDutyByOrgId(orgId) {
        //load the duty
        $.ajax({
            type:"get",
            url:"/ajax/duty-list.xhtml?organizationId=" + orgId,
            //data: "name=John&location=Boston",
            dataType:"json",
            success:function (data) {
                var strHTML = "<option value=\"-1\">所有职务</option>";
                if (data != null && data.length > 0) {
                    $.each(data, function (i, duty) {
                        strHTML += "<option value=\"" + duty.id + "\">" + duty.dutyName + "</option>";
                    });
                }
                $("#duty_sel").prev("span").html("所有职务");
                $("#duty_sel").html("");
                $("#duty_sel").append(strHTML);
            }
        });
    }

    function onSearchClick(){
        var year = $("#search_year_sel").val();
        var month = $("#search_month_sel").val();
        var orgs = $("#org_sel").children("div");
        var orgId = -1;
        if(orgs != null && orgs.length > 0){
            var last = orgs[orgs.length - 1];
            if($(last).find("span").html() == "所有部门"){
                var divId = $(last).attr("id"); //uniform-undefined-4
                orgId = divId.substr(divId.lastIndexOf("-")+1, divId.length);
            }else{
                orgId = $(last).find("select").val();
            }
            if(orgId == undefined || orgId == "undefined"){
                orgId = -1;
            }
        }
        var dutyId = $("#duty_sel").val();

        var data = {"startDateStr":$("#startDate").val(), "endDateStr":$("#endDate").val(), "orgId":orgId, "dutyId":dutyId, "organizationId":"${organizationId}", "search":"true"};
        doSearch(data);
    }
</script>